package com.example.petplatform.exception;

import com.example.petplatform.common.Result;
import jakarta.servlet.http.HttpServletRequest;
import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常处理
 *
 */
@ControllerAdvice(basePackages = "com.example.petplatform.controller")
public class GlobalExceptionHandler {
   private static final Logger log=LoggerFactory.getLogger(GlobalExceptionHandler.class);
    //捕获系统异常
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result error(HttpServletRequest request, Exception e){
        log.error(() -> "异常信息：", e);
        return Result.error("系统异常");
    }
    //捕获自定义异常
    @ExceptionHandler(CustomException.class)
    @ResponseBody
    public Result customError(HttpServletRequest request,CustomException e){
        return Result.error(e.getMessage());
    }
}
